package com.hd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hd.service.ActivityService;
import com.hd.dto.ActivityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import result.ResponseDO;
import com.hd.vo.ActivityVO;

import java.util.Map;


@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    /**
     * 初始化数据获取
     * @return
     */
    @GetMapping("/info/init")
    public ResponseDO init(){
        Map<String, Object> initMap = activityService.init();
        return ResponseDO.success(initMap);
    }

    /**
     * 提交活动数据
     * @param activityVO
     * @return
     */
    @PostMapping("/info/submit")
    public ResponseDO submit(@RequestBody ActivityVO activityVO){
        if (activityVO.getTitle() == null || activityVO.getTitle().isEmpty()) {
            return ResponseDO.fail("活动标题不能为空");
        }

        if (activityVO.getTitle().length() > 32) {
            return ResponseDO.fail("活动标题长度不能大于32个字符");
        }

        if (activityVO.getCategoryId()>5&&activityVO.getCategoryId()!=9) {
            return ResponseDO.fail("分类信息不正确");
        }

        if (activityVO.getContent() == null || activityVO.getContent().isEmpty()) {
            return ResponseDO.fail("活动内容不能为空");
        }

        if (activityVO.getHighLight().length() > 150) {
            return ResponseDO.fail("活动亮点不能超过150字");
        }

        if (activityVO.getContent().length() < 50) {
            return ResponseDO.fail("活动详情不能少于50字");
        }

        if (activityVO.getAddressType() != 1 && activityVO.getAddressType() != 2) {
            return ResponseDO.fail("活动地址模式不正确");
        }

        if (activityVO.getAddressType() == 2 && (activityVO.getAddress() == null ||activityVO.getAddress().trim().isEmpty())) {
            return ResponseDO.fail("线下活动地址不能为空");
        }

        if (activityVO.getMostJoinNum() <= 0 || activityVO.getLeastJoinNum() <= 0) {
            return ResponseDO.fail("最大人数或最小人数输入无效");
        }
        if (activityVO.getMostJoinNum() < activityVO.getLeastJoinNum()) {
            return ResponseDO.fail("最大人数小于最小人数");
        }
        if (activityVO.getMostJoinNum() > 500) {
            return ResponseDO.fail("活动人数不能超过500");
        }

        if (activityVO.getAddress() != null && activityVO.getAddress().length() > 100) {
            return ResponseDO.fail("活动地址信息不能超过100字");
        }

        if (activityVO.getBeginTime().before(activityVO.getEndTime())) {
            return ResponseDO.fail("活动开始时间不能小于截止时间");
        }
        ActivityDTO activityDTO = new ActivityDTO();
        BeanUtil.copyProperties(activityVO,activityDTO);
        int activityId = activityService.submit(activityDTO);
        //返回插入的活动id
        return ResponseDO.success(activityId);
    }
}
